/*
 * getCustomer.java
 *
 * Created on April 4, 2007, 8:41 PM
 */

package edu.byu.isys413.cbb54.intex2kb.views;
import edu.byu.isys413.cbb54.intex2kb.data.*;
import java.util.List;

/**
 *
 * @author  tylerf
 */
public class getCustomer extends javax.swing.JFrame {
    
    Employee employee = null;
    Store store = null;
    
    /** Creates new form getCustomer */
    public getCustomer() {
        initComponents();
        createCust.setVisible(false);
        custPhoneInput.setText("8013786198");
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        custPhoneInput = new javax.swing.JTextField();
        submit = new javax.swing.JButton();
        createCust = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jLabel1.setText("Customer Phone Number:");

        submit.setText("Submit");
        submit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitActionPerformed(evt);
            }
        });

        createCust.setText("Create Cust");
        createCust.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createCustActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(141, 141, 141)
                        .add(submit)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 22, Short.MAX_VALUE)
                        .add(createCust))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(custPhoneInput, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(custPhoneInput, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(submit)
                    .add(createCust))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void createCustActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createCustActionPerformed
        this.dispose();
        customerCreate c = new customerCreate();
        c.setVisible(true);
    }//GEN-LAST:event_createCustActionPerformed
    
    private void submitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitActionPerformed
        try {
            Customer cust = null;
            List<Customer> custList = CustomerDAO.getInstance().getByPhone(custPhoneInput.getText());
            if (custList.size() > 0){
                cust = custList.get(0);
                
                employee = Session.getInstance().getEmployee();
                store = Session.getInstance().getStore();
               
                Main m = new Main(employee.getId(), store.getId(), cust);
                this.dispose();
                m.setVisible(true);
                
            }else{
                custPhoneInput.setText("No customer found");
                customerCreate c = new customerCreate();
                this.dispose();
                c.setVisible(true);
            }
            
            
        } catch (DataException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_submitActionPerformed
    
    /**
     * @param args the command line arguments
     */
    /*public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new getCustomer().setVisible(true);
            }
        });
    }*/
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton createCust;
    private javax.swing.JTextField custPhoneInput;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton submit;
    // End of variables declaration//GEN-END:variables
    
}
